package com._21cn._admin.authority;

import com._21cn._admin.utils.AdminAuthHelper;
import com._21cn.cms.common.utils.ResponseUtil;
import com._21cn.framework.http.HttpRequestInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2017/9/9.
 */
@Controller
@RequestMapping("/api/authority")
public class AuthorityController {

    private static String redirectUrl = "/jsp/admin/index.html";

    @Autowired
    private AdminAuthHelper adminAuthHelper;

    @RequestMapping("/login")
    public void login(HttpServletRequest request, HttpServletResponse response, ModelMap model){
        try {
            response.setCharacterEncoding("UTF-8");
            HttpRequestInfo reqInfo = new HttpRequestInfo(request);
            boolean isLogin = adminAuthHelper.isLogin(reqInfo);
            if( isLogin ){
                response.sendRedirect(redirectUrl);
                return;
            }else {
                String username = reqInfo.getParameter("username", "");
                String password = reqInfo.getParameter("password", "");
                if( "adminQiaoliba".equals(username) && "gzQiaoliba.com".equals(password) ){
                    adminAuthHelper.loginSetting(request, response);
                    response.sendRedirect(redirectUrl);
                    return;
                }else {
                    ResponseUtil.returnJsonMsg(response, 1, "账号or密码错误", null);
                    return;
                }

            }
        }catch (Exception e){
            ResponseUtil.returnJsonMsg(response, 1, "服务器处理错误", null);
            return;
        }
    }
}
